﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
//using Printing.DBUtility;
using System.Data.SqlClient;

namespace MINERP.DAL.PrintManage
{
    public class BoardDAL:DALBase
    {
        public static List<Board> GetBoardPrintingList(int id)
        {
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                return Entities.Board.Where(b=>b.status ==1&&b.printer.PrinterID ==id).ToList();
            }
        }

        public static Board GetBoardObj(int id)
        {
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                Board board = Entities.Board.Include("picMakeUp").First(b => b.BoardId == id);
                Entities.Connection.Close();
                return board;
            }

        }

        public static Board GetBoardObj(string name)
        {
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                Board board = null;
                
                board = Entities.Board.FirstOrDefault(b => b.picPath.Contains(name));
                Entities.Connection.Close();
                return board;

            }
            //return board;
        }

        /// <summary>
        /// 得到某大板上所有的照片所对应的产品的所有照片都完成的产品ID列表
        /// </summary>
        /// <param name="boaridid"></param>
        /// <returns></returns>
        public List<int> GetPrintedPhotoOrderProductByBoardid(int boaridid)
        {
            List<int> OverOIID = new List<int>();
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("CheckBoardPhotosIsCompleted");
            db.AddInParameter(dbCommand, "@boardid", DbType.Int32, boaridid);
            using (IDataReader dr=db.ExecuteReader(dbCommand))
            {
                while (dr.Read())
                {
                    OverOIID.Add(dr.GetInt32(0));
                }
            }
            return OverOIID;
        }

        public bool ChangeBoardAndPhotoAndWorkOrderFlowList(int boaridid,int paperFaceId,int paperTypeId,int techID,int OperUid)
        {
            List<int> OverOIID = new List<int>();
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UpdateBPStatusAndChangeFlowList");
            db.AddInParameter(dbCommand, "@boardid", DbType.Int32, boaridid);
            db.AddInParameter(dbCommand, "@paperFaceId", DbType.Int32, paperFaceId);
            db.AddInParameter(dbCommand, "@paperTypeID", DbType.Int32, paperTypeId);
            db.AddInParameter(dbCommand, "@techid", DbType.Int32, techID);
            db.AddInParameter(dbCommand, "@operUid", DbType.Int32, OperUid);

            int result = (int)db.ExecuteScalar(dbCommand);

            if (result==0)
            {
                return false;
            }
            else
            {
                return true;
            }          
        }

        public static List<int> GetListBoardId(string customerName)
        {
            string sql = string.Format("select distinct d.BoardId from orderInfo a,photoINfo b,picmakeup c,board d where a.orderproductid =b.orderproductid and b.photoid =c.photoid and c.boardid =d.boardid and a.ordername like'%{0}%' ", customerName);
            return DataBaseAccess(sql);
        }

        private static List<int> DataBaseAccess(string sqlstr)
        {
            List<int> list = new List<int>();
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sqlstr);
            IDataReader rdr = db.ExecuteReader(cmd);
            while (rdr.Read())
            {
                list.Add(rdr.GetInt32(0));
            }
            return list;
        }
    }
}
